/**
 * Project Name:jrx
 * File Name:AppBaseController.java
 * Package Name:cn.bluemobi.jrx.core.web
 * Date:2015年7月10日下午4:05:24
 * Copyright (c) 2015, bluemobi.cn All Rights Reserved.
 *
*/

package cn.bluemobi.app.core.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluemobi.common.core.dto.Dto;
import cn.bluemobi.common.core.utils.BlueMobiConstants;

/**
 * Description: app接口基础类。<br/>
 * Date: 2015年7月10日 下午4:05:24 <br/>
 * 
 * @author hut
 * @version
 * @see
 */
public class AppBaseController extends BaseController {

    protected static final Logger log = LogManager.getLogger(AppBaseController.class);

    /**
     * 如果发生异常，将默认返回错误json Description: <br/>
     */
    @ExceptionHandler(Throwable.class)
    @ResponseBody
    protected Dto<Object> handleException(Exception ex) {
        log.error(ExceptionUtils.getStackTrace(ex));

        Dto<Object> data = new Dto<Object>();
        data.setStatus(BlueMobiConstants.FAIL);
        data.setMsg(ExceptionUtils.getMessage(ex));
        return data;
    }

    /**
     * 为方便前端调试，开发期间，打开跨域设置
     */
    @ModelAttribute
    public void addHeader(HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Origin", "*");
    }

}
